# the minimum version of CMake.
cmake_minimum_required(VERSION 3.7)

project(ohos_webrtc)

set(CMAKE_CXX_STANDARD 17)

if(DEFINED PACKAGE_FIND_FILE)
    include(${PACKAGE_FIND_FILE})
endif()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    message(STATUS "SDK_NATIVE_VERSION: ${SDK_NATIVE_VERSION}")
    message(STATUS "OHOS_PLATFORM_LEVEL: " ${OHOS_PLATFORM_LEVEL})
    # message(STATUS "SDK_NATIVE_MIN_PLATFORM_LEVEL: " ${SDK_NATIVE_MIN_PLATFORM_LEVEL})
    # message(STATUS "SDK_NATIVE_MAX_PLATFORM_LEVEL: " ${SDK_NATIVE_MAX_PLATFORM_LEVEL})
    # message(STATUS "OHOS_SDK_NATIVE_PLATFORM_LEVEL: " ${OHOS_SDK_NATIVE_PLATFORM_LEVEL})

    # 获取API版本（仅用于调试）
    if (${CMAKE_VERSION} VERSION_GREATER 3.19)
        file(READ "${OHOS_SDK_NATIVE}/oh-uni-package.json" OH_UNI_PACKAGE)
        string(JSON SDK_API_VERSION GET ${OH_UNI_PACKAGE} "apiVersion")
    else()
        file(STRINGS "${OHOS_SDK_NATIVE}/oh-uni-package.json" API_VER REGEX "\"apiVersion\":.*")
        string(REGEX REPLACE "\"apiVersion\":(.*),$" "\\1" SDK_API_VERSION "${API_VER}")
        string(STRIP "${SDK_API_VERSION}" SDK_API_VERSION)
        # 去掉前后的双引号
        string(REGEX REPLACE "\"([^\"]*)\"" "\\1" SDK_API_VERSION "${SDK_API_VERSION}")
    endif()

    message(STATUS "SDK_API_VERSION: " ${SDK_API_VERSION})

    # 去掉前后的双引号
    string(REGEX REPLACE "\"([^\"]*)\"" "\\1" SDK_NATIVE_VERSION "${SDK_NATIVE_VERSION}")
    if(SDK_NATIVE_VERSION VERSION_GREATER_EQUAL "4.1")
        message(STATUS "SDK_NATIVE_VERSION greater than or equal to 4.1")
    elseif(SDK_NATIVE_VERSION VERSION_GREATER_EQUAL "4.0")
        message(STATUS "SDK_NATIVE_VERSION less than 4.1 and greater than or equal to 4.0")
    else()
        message(ERROR "SDK_NATIVE_VERSION less than 4.0")
    endif()
endif()

set(THIRDPARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rd)
set(THIRDPARTY_WEBRTC_HEADER_PATH ${THIRDPARTY_PATH}/webrtc/include)
set(THIRDPARTY_NAPI_HEADER_PATH ${THIRDPARTY_PATH}/node-addon-api)

file(GLOB_RECURSE SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/*.cpp)

set(NDK_LIBS libace_napi.z.so libhilog_ndk.z.so)
list(APPEND NDK_LIBS ohaudio)
list(APPEND NDK_LIBS ohcamera ohimage image_receiver native_buffer native_window native_image EGL GLESv3)
list(APPEND NDK_LIBS native_media_codecbase native_media_core native_media_venc native_media_vdec native_avscreen_capture)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    message(STATUS "SRCS:" ${SRCS})
    message(STATUS "NDK_LIBS:" ${NDK_LIBS})
endif()

add_library(ffmpeg SHARED IMPORTED)
set_target_properties(ffmpeg
    PROPERTIES
    IMPORTED_LOCATION ${THIRDPARTY_PATH}/ffmpeg/libs/${OHOS_ARCH}/libffmpeg.so)

add_library(${PROJECT_NAME} SHARED ${SRCS})

target_link_libraries(${PROJECT_NAME} PRIVATE ${NDK_LIBS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${THIRDPARTY_PATH}/webrtc/libs/${OHOS_ARCH}/libwebrtc.a)
target_link_libraries(${PROJECT_NAME} PRIVATE ffmpeg)

target_compile_definitions(${PROJECT_NAME} PRIVATE WEBRTC_POSIX WEBRTC_LINUX WEBRTC_USE_H264)
target_compile_definitions(${PROJECT_NAME} PRIVATE NODE_ADDON_API_DISABLE_DEPRECATED)

target_compile_options(${PROJECT_NAME} PRIVATE -fno-rtti)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_options(${PROJECT_NAME} PRIVATE -fexceptions)
    target_compile_definitions(${PROJECT_NAME} PRIVATE NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS)
else()
    target_compile_definitions(${PROJECT_NAME} PRIVATE NAPI_DISABLE_CPP_EXCEPTIONS NDK_HELPER_DISABLE_CPP_EXCEPTIONS)
endif()

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
target_include_directories(${PROJECT_NAME} PRIVATE ${THIRDPARTY_NAPI_HEADER_PATH})
target_include_directories(${PROJECT_NAME} PRIVATE ${THIRDPARTY_WEBRTC_HEADER_PATH}/ohos_webrtc/)
target_include_directories(${PROJECT_NAME} PRIVATE ${THIRDPARTY_WEBRTC_HEADER_PATH}/ohos_webrtc/third_party/abseil-cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${THIRDPARTY_WEBRTC_HEADER_PATH}/ohos_webrtc/third_party/libyuv/include)
